package com.dbsync.core;

import com.dbsync.exception.DbsyncException;

import java.sql.Connection;
import java.sql.DriverManager;
import java.util.List;

/**
 * 数据库工具类
 */
public class DBUtil {
    // 配置文件对象
    private Config config;
    // 源数据库连接
    private Connection dbConnection;
    // 任务数据库连接
    private Connection targetConnection;

    /**
     * 初始化
     * @throws Exception
     */
    public void init() throws Exception {
        this.config = ConfigLoader.init();
    }

    /**
     * 获取任务集合
     * @return
     */
    public List<Target> getTargets() {
        return this.config.getTargets();
    }

    /**
     * 获取源数据库连接
     * @return 返回connection对象
     * @throws Exception
     */
    public Connection getDBConnection() throws Exception {
        if (this.dbConnection==null||this.dbConnection.isClosed()){
            Database database = this.config.getDatabase();
            Class.forName(database.getDriverClassName());
            this.dbConnection = DriverManager.getConnection(
                    database.getUrl(),
                    database.getUsername(),
                    database.getPassword()
            );
        }
        return this.dbConnection;
    }

    /**
     * 关闭源数据库连接
     * @throws Exception
     */
    public void closeDB() throws Exception {
        if (this.dbConnection!=null&&!this.dbConnection.isClosed()){
            this.dbConnection.close();
        }
    }

    /**
     * 获取任务数据库连接
     * @param index 任务索引
     * @return 返回connection对象
     * @throws Exception
     */
    public Connection getTargetConnection(int index) throws Exception {
        if (this.targetConnection==null||!this.targetConnection.isClosed()){
            Target target = this.config.getTargets().get(index);
            Class.forName(target.getDriverClassName());
            this.targetConnection = DriverManager.getConnection(
                    target.getUrl(),
                    target.getUsername(),
                    target.getPassword()
            );
        }
        return this.targetConnection;
    }

    /**
     * 关闭任务数据库连接
     * @throws Exception
     */
    public void closeTarget() throws Exception {
        if (this.targetConnection!=null&&!this.targetConnection.isClosed()){
            this.targetConnection.close();
        }
    }

    /**
     * 获取任务数据库连接(数据插入时使用)
     * @param index 任务索引
     * @return 返回connection对象
     * @throws Exception
     */
    public Connection getMoreTargetConnection(int index) throws Exception {
        Target target = this.config.getTargets().get(index);
        Class.forName(target.getDriverClassName());
        return DriverManager.getConnection(
                target.getUrl(),
                target.getUsername(),
                target.getPassword()
        );
    }

    /**
     * 获取数据库类型
     * @param index 任务索引
     * @return 返回对应数据库类型
     * @throws DbsyncException
     */
    public String getDBType(int index) throws DbsyncException{
        if (index < 1) {
            return this.config.getDatabase().getType();
        } else {
            return this.config.getTargets().get(index).getType();
        }
    }

    /**
     * 获取源数据库名称
     * @return 返回源数据库名称
     */
    public String getDBName(){
        String dbUrl = this.config.getDatabase().getUrl();
        if (dbUrl.contains("?")){
            return dbUrl.substring(dbUrl.lastIndexOf("/")+1, dbUrl.indexOf("?"));
        }else{
            return dbUrl.substring(dbUrl.lastIndexOf("/")+1);
        }
    }

    /**
     * 根据任务索引获取任务数据名称
     * @param index 任务索引
     * @return 返回任务数据库名称
     */
    public String getTargetName(int index){
        String targetUrl = this.config.getTargets().get(index).getUrl();
        if (targetUrl.contains("?")){
            return targetUrl.substring(targetUrl.lastIndexOf("/")+1, targetUrl.indexOf("?"));
        }else{
            return targetUrl.substring(targetUrl.lastIndexOf("/")+1);
        }
    }
}